package cn.itcast.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Reference
    private PermissionService permissionService;


    @PutMapping
    public ResponseEntity<Result> edit(@RequestBody Permission permission) {
        Result result = new Result();
        try {
            Boolean flag = permissionService.edit(permission);
            if (flag) {
                result.setFlag(true);
                result.setMessage(MessageConstant.EDIT_PERMISSION_SUCCESS);
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.error(MessageConstant.EDIT_PERMISSION_FAIL, e);
        }
        result.setFlag(false);
        result.setMessage(MessageConstant.EDIT_PERMISSION_FAIL);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Result> delete(@PathVariable Integer id) {
        Result result = new Result();
        try {
            Boolean flag = permissionService.delete(id);
            if (flag) {
                result.setFlag(true);
                result.setMessage(MessageConstant.DELETE_PERMISSION_SUCCESS);
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.error(MessageConstant.DELETE_PERMISSION_FAIL, e);
        }
        result.setFlag(false);
        result.setMessage(MessageConstant.DELETE_PERMISSION_FAIL);
        return ResponseEntity.ok(result);
    }


    /**
     * 新建权限
     *
     * @return
     */
    @PostMapping
    public ResponseEntity<Result> savePermission(@RequestBody Permission permission) {
        Result result = new Result();
        try {
            Boolean flag = permissionService.save(permission);
            if (flag) {
                result.setFlag(true);
                result.setMessage(MessageConstant.ADD_PERMISSION_SUCCESS);
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.error(MessageConstant.ADD_PERMISSION_FAIL, e);
        }
        result.setFlag(false);
        result.setMessage(MessageConstant.ADD_PERMISSION_FAIL);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/all")
    public ResponseEntity<Result> findAll() {
        Result result = new Result();
        try {
            List<Permission> permissionList = permissionService.findAll();
            if (ObjectUtil.isNotEmpty(permissionList)) {
                result.setFlag(true);
                result.setMessage(MessageConstant.QUERY_PERMISSION_SUCCESS);
                result.setData(permissionList);
                return ResponseEntity.ok(result);
            }

        } catch (Exception e) {
            log.error(MessageConstant.QUERY_PERMISSION_FAIL, e);
        }
        result.setFlag(false);
        result.setMessage(MessageConstant.QUERY_PERMISSION_FAIL);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询权限
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @GetMapping
    public ResponseEntity<Result> findByPage(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                             @RequestParam(value = "queryString", required = false) String queryString) {
        Result result = new Result();
        try {
            PageResult pageResult = permissionService.findByPage(currentPage, pageSize, queryString);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                result.setFlag(true);
                result.setMessage(MessageConstant.QUERY_PERMISSION_SUCCESS);
                result.setData(pageResult);
                return ResponseEntity.ok(result);
            }

        } catch (Exception e) {
            log.error(MessageConstant.QUERY_PERMISSION_FAIL, e);
        }
        result.setFlag(false);
        result.setMessage(MessageConstant.QUERY_PERMISSION_FAIL);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Result> findById(@PathVariable Integer id) {
        Result result = new Result();
        try {
            Permission permission = permissionService.findById(id);
            if (ObjectUtil.isNotEmpty(permission)) {
                result.setFlag(true);
                result.setMessage(MessageConstant.QUERY_PERMISSION_SUCCESS);
                result.setData(permission);
                return ResponseEntity.ok(result);
            }

        } catch (Exception e) {
            log.error(MessageConstant.QUERY_PERMISSION_FAIL, e);
        }
        result.setFlag(false);
        result.setMessage(MessageConstant.QUERY_PERMISSION_FAIL);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/menu/association")
    public ResponseEntity<Result> findByMenuIds(@RequestParam("menuIds") List<Integer> menuIds) {
        Result result = new Result();
        try {
            List<Permission> permissionList = permissionService.findByMenuIds(menuIds);
            if (ObjectUtil.isNotEmpty(permissionList)) {
                result.setFlag(true);
                result.setMessage(MessageConstant.QUERY_PERMISSION_SUCCESS);
                result.setData(permissionList);
                return ResponseEntity.ok(result);
            }

        } catch (Exception e) {
            log.error(MessageConstant.QUERY_PERMISSION_FAIL, e);
        }
        result.setFlag(false);
        result.setMessage(MessageConstant.QUERY_PERMISSION_FAIL);
        return ResponseEntity.ok(result);
    }
}
